package com.visiontech.volunteer_center.service.impl;

import com.visiontech.volunteer_center.mapper.UserMapper;
import com.visiontech.volunteer_center.pojo.User;
import com.visiontech.volunteer_center.service.UserService;
import com.visiontech.volunteer_center.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String verifyUser(String account, Integer roleId, String password) {
        User user = userMapper.findByAccount(account, roleId);// 赋给User变量直接使用User变量就不用多次查询数据库
        if(user == null){
            return null;
        } else {
            password = MD5Util.encrypt(password);
            if(user.getPassword().equals(password)){
                return "success";
            } else {
                return "error";
            }
        }
    }

    @Override
    public Integer insertUser(String userName, String account, Integer roleId, String password, String email, String phone) {
        if(userMapper.findUserId(account) == null){
            password = MD5Util.encrypt(password);
            Integer result = userMapper.insertUser(userName, account, password, email, phone);
            userMapper.insertUser_Role(userMapper.findUserId(account), roleId);
            return result;
        } else{
            if(Objects.isNull(userMapper.findByAccount(account, roleId))){
                return userMapper.insertUser_Role(userMapper.findUserId(account), roleId);
            }
            else return 0;
        }
    }
}
